//
//  ViewController.m
//  TableViewApp
//
//  Created by Dmitriy on 04.05.13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "Task.h"
#import "TaskCollector.h"
#import "TaskDetailsViewController.h"

@interface ViewController()

@end


@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    tasks = [TaskCollector sharedInstance];
    return self;
}

- (void) dealloc
{
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tasks getAmountOfTasks];
}

- (UITableViewCell *) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"CellIdentifier";
    UITableViewCell* cell;
    cell = [_tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];
    }
    Task *taskToFill = [[TaskCollector sharedInstance] getTaskWithIndex:[indexPath row]];
    cell.textLabel.text = taskToFill.taskName;
    cell.detailTextLabel.text = [NSDateFormatter localizedStringFromDate:taskToFill.taskTime dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];

    return cell;
}

//-(void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [self performSegueWithIdentifier:@"Details" sender:[_tableView cellForRowAtIndexPath:indexPath]];
//}

- (void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[TaskCollector sharedInstance] deleteTaskWithIndex:[indexPath row]];
    [_tableView reloadData];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    Task* taskForEditing;
    if(sender!=nil)
    {
        UITableViewCell* cell = (UITableViewCell*)sender;
        [segue.destinationViewController setTitle:cell.textLabel.text];
        int cellIndex = [[tableView indexPathForCell:cell] row];
        taskForEditing = [[TaskCollector sharedInstance]getTaskWithIndex:cellIndex];
    }
    else
    {
        [segue.destinationViewController setTitle:@"New task"];
        taskForEditing = nil;
    }
    [segue.destinationViewController setEditingTask:taskForEditing];
}

-(IBAction)addNewTask:(id)sender
{
//    Task *taskToAdd = [[Task alloc]initWithName:@"Enter task name" time:[NSDate date]];
//    TaskCollector *instance = [TaskCollector sharedInstance];[instance appendTask:taskToAdd];
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[instance getAmountOfTasks]-1 inSection:0];
//    [tableView reloadData];
    [self performSegueWithIdentifier:@"Details" sender:nil];
}

@end
